package com.oceansoft.core.jxkh.dao.impl;

import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.SysDept;
import com.oceansoft.core.jxkh.dao.IDepartmentDao;
import com.oceansoft.core.jxkh.domain.Department;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by OS on 2017/12/5.
 */

@Repository
public class DepartmentDaoImpl implements IDepartmentDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Pagination findAllDepartmentInfo(HashMap<String,String> map) {
        int offset = Integer.parseInt(map.get("offset").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        String sql = "select t.*,(select t1.name from t_sys_dept t1 where t1.deptId = t.parentId) parentName from t_sys_dept t order by dbid";
        String count_sql = PageUtil.getCountSQL(sql.toString());
        int _total = jdbcTemplate.queryForInt(count_sql);
        String page_sql = PageUtil.getPageSQL(sql.toString(), offset,limit);
        List<SysDept> list = jdbcTemplate.query(page_sql, new RowMapper<SysDept>() {
            @Override
            public SysDept mapRow(ResultSet rs, int arg1) throws SQLException {
                SysDept sysDept = new SysDept();
                sysDept.setDbId(rs.getInt("dbid"));
                sysDept.setName(rs.getString("name"));
                sysDept.setDeptId(rs.getString("deptId"));
                sysDept.setOrderNum(rs.getInt("orderNum"));
                sysDept.setParentId(rs.getString("parentId"));
                sysDept.setParentName(rs.getString("parentName"));
                return sysDept;
            }
        });
        return new Pagination(list,_total,limit, (offset/limit)+1);
    }

    @Override
    public void editDepartmentInfo(SysDept sysDept) {
        String sql = "update t_sys_dept set name = ? where dbid = ?";
        jdbcTemplate.update(sql,sysDept.getName(),sysDept.getDbId());
    }

    @Override
    public SysDept findDepartmentInfoByDbid(int dbid) {
        String sql = "select * from t_sys_dept where dbid = ?";
        SysDept sysDept = jdbcTemplate.queryForObject(sql, new Object[]{dbid}, new RowMapper<SysDept>() {
            @Override
            public SysDept mapRow(ResultSet rs, int args) throws SQLException {
                SysDept sysDept = new SysDept();
                sysDept.setDbId(rs.getInt("dbid"));
                sysDept.setName(rs.getString("name"));
                sysDept.setDeptId(rs.getString("deptId"));
                sysDept.setOrderNum(rs.getInt("orderNum"));
                sysDept.setParentId(rs.getString("parentId"));
                return sysDept;
            }
        });
        return sysDept;
    }

    @Override
    public Pagination findDepartmentInfoByDeptId(HashMap<String, String> map) {
        int offset = Integer.parseInt(map.get("offset").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        String sql = "select t.*,(select t1.name from t_sys_dept t1 where t1.deptId = t.parentId) parentName from t_sys_dept t where deptId = ? order by dbid";
        String count_sql = PageUtil.getCountSQL(sql.toString());
        int _total = jdbcTemplate.queryForInt(count_sql,map.get("deptId"));
        String page_sql = PageUtil.getPageSQL(sql.toString(), offset,limit);
        List<SysDept> list = jdbcTemplate.query(page_sql,new Object[]{map.get("deptId")},new RowMapper<SysDept>() {

            @Override
            public SysDept mapRow(ResultSet rs, int args) throws SQLException {
                SysDept sysDept = new SysDept();
                sysDept.setDbId(rs.getInt("dbid"));
                sysDept.setName(rs.getString("name"));
                sysDept.setDeptId(rs.getString("deptId"));
                sysDept.setOrderNum(rs.getInt("orderNum"));
                sysDept.setParentId(rs.getString("parentId"));
                sysDept.setParentName(rs.getString("parentName"));
                return sysDept;
            }
        });
        return new Pagination(list,_total,limit, (offset/limit)+1);
    }

    @Override
    public void deleteDepartmentInfoByDbid(int dbid) {
        String sql = "delete from t_sys_dept where dbid = ?";
        jdbcTemplate.update(sql,dbid);
    }
}
